package com.linsir.lin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDto;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.linsir.base.SearchPara;
import com.linsir.core.BaseResponse;
import com.linsir.core.CodeEnum;
import com.linsir.lin.dto.GridDTO;
import com.linsir.lin.dto.MetaObjectDTO;
import com.linsir.lin.mapper.MetaObjectMapper;
import com.linsir.lin.pojo.MetaObject;
import com.linsir.lin.service.BaseCodeService;
import com.linsir.lin.service.MetaObjectService;
import com.linsir.utils.StringUtils;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @ProjectName:linsir-lin
 * @Package:com.linsir.lin.service
 * @Title: MetaObjectServiceImpl
 * @Description: TODO
 * @Create:2021/6/5 22:27 by @author:Linsir
 * @Update:2021/6/5 22:27 by @author:Linsir
 * @UpdateRemark: 初始创建
 * @Version 1.0.0
 */
@Service
public class MetaObjectServiceImpl extends ServiceImpl<MetaObjectMapper,MetaObject> implements MetaObjectService,IService<MetaObject>{

    @Autowired
    private BaseCodeService baseCodeService;

    @Override
    public BaseResponse<MetaObject> add(MetaObject metaObject) {

        if (super.saveOrUpdate(metaObject))
        {
            return BaseResponse.success("增加成功",metaObject);
        }
        else
        {
            return BaseResponse.success("增加失败",null);
        }

    }

    /**
     * @param page
     * @param limit
     * @param searchParams
     * @return
     */
    @Override
    public GridDTO<MetaObjectDTO> grid(String page,
                                    String limit,
                                    String searchParams) throws IOException, InvocationTargetException, IllegalAccessException {
        Map<String,String> map = SearchPara.toMap(searchParams);

        QueryWrapper<MetaObject> wrapper=new QueryWrapper<MetaObject>();

        if (map!=null)
        {
            wrapper.eq("view_name",map.get("viewName"));
        }

        Page<MetaObject> pageDto = new PageDto<MetaObject>();
        pageDto.setCurrent(Long.parseLong(page));
        pageDto.setSize(Long.parseLong(limit));
        pageDto = super.baseMapper.selectPage(pageDto,wrapper);
        List<MetaObjectDTO> metaObjectDTOList= toDtoList(pageDto.getRecords());
        return new GridDTO<MetaObjectDTO>("0","成功",pageDto.getTotal(),metaObjectDTOList);
    }

    /**
     * 从pojo 到 DTO
     *
     * @param metaObject
     * @return
     */
    @Override
    public MetaObjectDTO toDto(MetaObject metaObject) throws InvocationTargetException, IllegalAccessException {
        MetaObjectDTO metaObjectDTO = new MetaObjectDTO();
        BeanUtils.copyProperties(metaObjectDTO,metaObject);
        String isCelleditName=baseCodeService.codeName("isCelledit",String.valueOf(metaObject.isCelledit()));
        String isSingleName=baseCodeService.codeName("isSingle",String.valueOf(metaObject.isSingle()));
        String isFirstLoad=baseCodeService.codeName("isFirstLoad",String.valueOf(metaObject.isFirstLoad()));
        String isShowNum=baseCodeService.codeName("isShowNum",String.valueOf(metaObject.isShowNum()));

        String leftCol=StringUtils.objectColLeft(metaObject);
        metaObjectDTO.setIsCelleditName(isCelleditName);
        metaObjectDTO.setIsSingleName(isSingleName);
        metaObjectDTO.setIsFirstLoadName(isFirstLoad);
        metaObjectDTO.setIsShowNumName(isShowNum);
        metaObjectDTO.setLeftCol(leftCol);
        return metaObjectDTO;
    }

    /**
     * @param metaObjectList
     * @return
     */
    @Override
    public List<MetaObjectDTO> toDtoList(List<MetaObject> metaObjectList) throws InvocationTargetException, IllegalAccessException {
        List<MetaObjectDTO> metaObjectDTOS =new ArrayList<MetaObjectDTO>();
        for (MetaObject metaObject : metaObjectList)
        {
            MetaObjectDTO metaObjectDTO =toDto(metaObject);
            metaObjectDTOS.add(metaObjectDTO);
        }
        return metaObjectDTOS;
    }

    /**
     * 根据id 获取 MetaObjectDTO 对象
     *
     * @param id
     * @return
     */
    @Override
    public MetaObjectDTO getMetaObjectDTOById(Integer id) throws InvocationTargetException, IllegalAccessException {
        return toDto(getById(id));
    }

    /**
     * 根据code 获取 MetaObjectDTO 对象
     *
     * @param code
     * @return
     */
    @Override
    public MetaObjectDTO getMetaObjectDTOByCode(String code) throws InvocationTargetException, IllegalAccessException {

        QueryWrapper<MetaObject> queryWrapper = new QueryWrapper<MetaObject>();
        queryWrapper.eq("code",code);
        MetaObject metaObject =getOne(queryWrapper);
        return toDto(metaObject);
    }

    /**
     * @param ids
     * @return
     */
    @Override
    public BaseResponse<Boolean> batchDel(String ids) {


        List<Integer> idList = StringUtils.strtOList(ids);

        if (removeByIds(idList))
        {
            return  new BaseResponse<Boolean>(CodeEnum.SUCCESS,"批量删除成功",true);
        }
        else
        {
            return  new BaseResponse<Boolean>(CodeEnum.FAILED,"批量删除失败",false);
        }

    }


}
